************************************************
* Sleep Project - Pedro Bessone, Gautam Rao, Heather Schofield, Frank Schilbach, and Mattie Toma
* Purpose: Replicates Appendix Table 10 (Impacts on Time Allocation)
* Last edited: 07 May 2021
************************************************


************************************************	
*0. Initial Setup
************************************************

	clear all
	set more off
	set seed 1

************************************************	
* 1. Opening datasets
************************************************


	use "$d/typing_dataset.dta", clear
	drop if day_in_study == . | day_in_study == 1
	
	merge 1:1 pid day_in_study using "$d/firststage_dataset.dta"
	

***************	 DECOMPOSING TABLES ***************************
eststo clear

*Transforming variables to minutes
preserve
	replace voluntary_pause =  voluntary_pause/60
	cap drop go_to_bed
	cap drop out_of_bed
	
	gen go_to_bed = hh( an_2_inbed_time ) + mm( an_2_inbed_time )/60
	replace go_to_bed = 24 + go_to_bed if go_to_bed < 10
	gen out_of_bed = hh(  an_4_outbed_time ) + mm(  an_4_outbed_time )/60
	
	

	* Define out_of_bed and go_to_bed variables as actigraph measures
	
	
foreach var in time_in_office checkin checkout voluntary_pause out_of_bed go_to_bed  {
	capture drop _baseline baseline
	gen _baseline			= `var' if post_treatment==0
	egen baseline 	= mean(_baseline), by(pid)
	
	local controls "female i.age long_day fraction_high baseline" 
	reghdfe `var' treat_pool treat_nap `controls' if post_treatment==1 & at_present_check==1, vce(cluster pid) absorb(date day_in_study)

	local pids_`var' = string(e(N_clust))
	local obs_`var' = string(e(N))
	
	lincom _b[treat_pool]
	local coef_ns_`var' = string(r(estimate),"%3.2f")
	local se_ns_`var' = string(r(se),"%3.2f")
	local p_ns_`var' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
	
	lincom _b[treat_nap]
	local coef_nap_`var' = string(r(estimate),"%3.2f")
	local se_nap_`var' = string(r(se),"%3.2f")
	local p_nap_`var' = (2 * ttail(e(df_r), abs(r(estimate)/r(se))))
				 
			* Significance - treat
			 if `p_ns_`var'' < 0.01 {
				local coef_ns_`var' = "`coef_ns_`var''***"
			}
			
			if `p_ns_`var'' < 0.05 & `p_ns_`var'' >= 0.01 {
				local coef_ns_`var' = "`coef_ns_`var''**"
			}
			
			 if `p_ns_`var'' < 0.1 & `p_ns_`var'' >= 0.05 {
				local coef_ns_`var' = "`coef_ns_`var''*"
			}
			
			* Significance - nap
			 if `p_nap_`var'' < 0.01 {
				local coef_nap_`var' = "`coef_nap_`var''***"
			}
			
			if `p_nap_`var'' < 0.05 & `p_nap_`var'' >= 0.01 {
				local coef_nap_`var' = "`coef_nap_`var''**"
			}
			
			if `p_nap_`var'' < 0.1 & `p_nap_`var'' >= 0.05 {
				local coef_nap_`var' = "`coef_nap_`var''*"
			}
	
	
	sum `var' if treatment_group == 0 & post_treatment == 1
	local mean_`var' = string(r(mean), "%3.2f")
	local sd_`var' = string(r(sd), "%3.2f")
	distinct(pid) if post_treatment == 1 & at_present_check==1

}

********** Creating Table ***********
		cd "$oa/Tables"
		file open f using "TableA10_decomposition_time_allocation.tex", write replace
			file write f "\begin{tabular}{l*{9}{c}}" _n ///
			"\toprule" _n ///
			" &\multicolumn{4}{c}{\textbf{Labor Supply}}&\multicolumn{2}{c}{\textbf{Sleep}}\\" _n ///
			"\cmidrule(lr){2-5}\cmidrule(lr){6-7} & \multicolumn{1}{c}{Time at Office}&\multicolumn{1}{c}{Arrival Time}&\multicolumn{1}{c}{Leave Time}& \multicolumn{1}{c}{Work Breaks} & \multicolumn{1}{c}{Get Up Time}&\multicolumn{1}{c}{Bed Time}\\" _n ///
			" &\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)}  &\multicolumn{1}{c}{(3)}  &\multicolumn{1}{c}{(4)}  &\multicolumn{1}{c}{(5)} &\multicolumn{1}{c}{(6)}       \\" _n ///
			"\midrule" _n ///
			"Night-Sleep Treatments & `coef_ns_time_in_office' & `coef_ns_checkin' & `coef_ns_checkout' & `coef_ns_voluntary_pause' & `coef_ns_out_of_bed' & `coef_ns_go_to_bed' \\" _n ///
			" & (`se_ns_time_in_office') & (`se_ns_checkin') & (`se_ns_checkout') & (`se_ns_voluntary_pause') & (`se_ns_out_of_bed') & (`se_ns_go_to_bed') \\" _n ///
			"Nap Treatment  & `coef_nap_time_in_office' & `coef_nap_checkin' & `coef_nap_checkout' & `coef_nap_voluntary_pause' & `coef_nap_out_of_bed' & `coef_nap_go_to_bed' \\" _n ///
			" & (`se_nap_time_in_office') & (`se_nap_checkin') & (`se_nap_checkout') & (`se_nap_voluntary_pause') & (`se_nap_out_of_bed') & (`se_nap_go_to_bed') \\" _n ///
			"\midrule" _n ///
			"Control Mean         & `mean_time_in_office' &  `mean_checkin' &  `mean_checkout' &   `mean_voluntary_pause'   & `mean_out_of_bed' & `mean_go_to_bed'    \\" _n ///
			"Control SD           & `sd_time_in_office'   &  `sd_checkin'   &  `sd_checkout'   &   `sd_voluntary_pause'     & `sd_out_of_bed'   & `sd_go_to_bed'    \\" _n ///
			"Participants         & `pids_time_in_office' &  `pids_checkin' &  `pids_checkout' &   `pids_voluntary_pause'   & `pids_out_of_bed' & `pids_go_to_bed' \\" _n ///
			"\bottomrule \\" _n ///
			"\end{tabular}" _n
		file close f
		
restore
